const express = require('express')
const router = express.Router()
const multer = require('multer')

const path = require('path')

// var upload = multer({ dest: 'static/uploads' }) // 上传之前目录必须存在

const storage = multer.diskStorage({
  // 配置上传的目录
  destination: function (req, file, cb) {
    cb(null, 'static/uploads')
  },
  // 修改上传后的问题件名
  filename: function (req, file, cb) {
    // 1. 获取后缀名
    let extname = path.extname(file.originalname)
    // 2. 根据时间戳生成文件名
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9) + extname
    cb(null, file.fieldname + '-' + uniqueSuffix)
  }
})

const upload = multer({ storage: storage })


router.get('/', (req, res) => {
  res.send("导航列表")
})

router.get('/add', (req, res) => {
  res.render('admin/nav/add', {})
  // res.send("增加导航")
})

router.get('/edit', (req, res) => {
  res.send("修改列表")
})

router.post('/doAdd', upload.single('pic'), (req, res) => {
  // 获取表单传过来的数据
  // res.send("执行增加：" + req.body.title + '-' + req.body.desc)

  res.send({
    body: req.body, 
    file: req.file
  })
})

router.post('/doEdit', (req, res) => {
  res.send("执行修改")
})

module.exports = router
